package shengjianan;





import java.util.ArrayList;


//经过修改,一个类对应一个玩家。
//*******2013-4-26    16:27*******
//   XXX O被设定成了单三，不予阻拦，纠正。

//特定的点的分数 


public class SingalScore {
	private int point=0;
	private int topX=0,topY=0;
	private final int max=1000000;
	private int numOfSingleFour,numOfDoubleFour,numOfSingleThree,numOfDoubleThree,numOfSingleTwo,numOfDoubleTwo,numOfBlankFour,numOfBlankThree,numOfBlankTwo;
	private JudgeNumber num=new JudgeNumber();
//	private int[][] points;   //points记录在某一点的分数
	private ArrayList<Chess> TopPoints=new ArrayList<Chess>(50);
	private boolean ifMustWin;      
	private boolean win;
	private IfWin ifWin; 
	private Color self;
//	private boolean ifCaled[][];    //ifCaled用于记录某一点是否已经计算过分数。可以减少remove判断所用的时间
	
	//获取棋盘初始信息
	public SingalScore(int inLine,int inArray,Color[][] input,Color color){
		num.setIni(inLine, inArray, input,color);
		ifWin=new IfWin(input,inLine,inArray);
//     	points=new int[inLine][inArray];
//     	ifCaled=new boolean[inLine][inArray];
     	TopPoints.add(new Chess(0,0,0));  //初始化最初的点
//     	System.out.println("the size of topPoint is: "+TopPoints.size());
     	self=color;
//	    	for(int i=0;i<inLine;i++)
//     		for(int j=0;j<inArray;j++){
//	    			points[i][j]=0;
////	    			ifCaled[i][j]=false;
//     		}
	}
	private void initial(){
		numOfSingleFour=0;
		numOfDoubleFour=0;
		numOfSingleThree=0;
		numOfDoubleThree=0;
		numOfSingleTwo=0;
		numOfDoubleTwo=0;
		numOfBlankFour=0;
		numOfBlankThree=0;
		numOfBlankTwo=0;
		point=0;
		ifMustWin=false;
		win=false;
	}
	
	
	//电脑需要计算全盘多个点的分数，而轮到玩家时，只需要计算最高分数以供提示和判断围堵。
	public void calculate(int x,int y){
		initial(); 
// 		System.out.println("calculate ("+(x+1)+","+(y+1)+")");   //显示出正在计算的点的坐标

		num.setXY(x, y);
		countNumber(num.numOfClinic());
		countNumber(num.numOfVertical());
		countNumber(num.numOfLeftGradient());
		countNumber(num.numOfRightGradient());
		
		ifMustWin=ifMustWin(numOfDoubleFour,numOfSingleFour,numOfDoubleThree);  
		win=ifWin.ifWin(x, y,self);
		if(ifMustWin==true||win==true)  {
			point=max;
//			System.out.println("("+(x+1)+","+(y+1)+")"+"ifMustWin:"+ifMustWin+"\nifWin:"+win);
		}
		else
     	    	point=calculate(numOfDoubleThree,numOfDoubleTwo,numOfSingleFour,numOfSingleThree,numOfSingleTwo,numOfBlankFour,numOfBlankThree,numOfBlankTwo);
		if(point>0){
	     	removePoint(x,y);         //如果已经计算过，则去除上一次计算的结果
         	setTopPoint(x,y);
		}
//		System.out.println("the length is "+TopPoints.size()+"\n the 0th element is "+TopPoints.get(0).getPoint());
			
//		if(x==2&&y==6)
//			System.out.println(point);
	}
	
	//保存所计算的点在所有分数中的位次
	private void setTopPoint(int x,int y){
		int size=TopPoints.size();
		for(int i=0;i<size;i++){
//			System.out.println("in the loop");
     		if(point>TopPoints.get(i).getPoint()){
	     		TopPoints.add(i,new Chess(x,y,point));
//				System.out.println("============================\n("+x+","+y+")"+"added"+"\n============================");
	     		break;
	     	}
		}
		if(size==0){
		TopPoints.add(new Chess(x,y,0));    
//		System.out.println("top point added"+"\nthe length is "+TopPoints.size());
		}
	}
	
	public int getTopPoint(){
		return TopPoints.get(0).getPoint();
	}
	
	//显示排序后的有分数的点
	public void showPoints(){
		for(int i=0;i<TopPoints.size();i++)
     		System.out.println("the points are ("+(TopPoints.get(i).getX()+1)+","+(TopPoints.get(i).getY()+1)+")");
		System.out.println("============================");
	}
	
	//删除已经被下子处的棋子分数记录
	public void removePoint(int x,int y){
//		showPoints();
//		System.out.println("loop delete");
		for(int i=0;i<TopPoints.size();i++){
			if((x==TopPoints.get(i).getX())&&(y==TopPoints.get(i).getY())){
//				System.out.println("location of deleted:"+"  "+i+"        \n("+TopPoints.get(i).getX()+","+TopPoints.get(i).getY()+")");
				TopPoints.remove(i);
//				System.out.println("("+x+","+y+")"+"deleted");
				break;
			}
		}
	}

	
	public int getTopX(){
	    	topX=TopPoints.get(0).getX();
      	return topX;
	}
	
	public int getTopY(){
	    	topY=TopPoints.get(0).getY();
      	return topY;
	}
	
	private boolean ifMustWin(int double4,int single4,int double3){
		if((double3>1)||(double4>0)||(single4>1)||((single4==1)&&(double3==1)))
			ifMustWin=true;
		return ifMustWin;
	}
	
	private void countNumber(Type input){
		switch(input){
		case doubleFour: numOfDoubleFour++;break;
		case doubleThree: numOfDoubleThree++;break;
		case doubleTwo: numOfDoubleTwo++;break;
		case singleFour: numOfSingleFour++;break;
		case singleThree: numOfSingleThree++;break;
		case singleTwo: numOfSingleTwo++;break;
		case blankFour: numOfBlankFour++;break;
		case blankThree: numOfBlankThree++;break;
		case blankTwo: numOfBlankTwo++;break;
		default:break;
		}
	}
	
	//singal类只有在多个double类分数相同时用于得出较优解
	//传递过多singal值是否影响过多时间有待观察
	//double3:10000,double2:2000,single4:500,single3:200,single2:50
	private int calculate(int double3,int double2,int single4,int single3,int single2,int blank4,int blank3,int blank2){
		int point=0;
		point=single4*20000+blank4*15000+double3*10000+double2*2000+single3*200+blank3*150+single2*50+blank2*25;
//		System.out.println("double3:"+double3+"  double2:"+double2+"  single4:"+single4+"  single3:"+single3+"  single2:"+single2);
		return point;
	}
}

